import requests
import time

class kuaidi_auto:

    def __init__(self, deliver_num):
        self.url = 'https://www.kuaidi100.com/autonumber/autoComNum?text=' + str(deliver_num)
        # self.user_agent = user_agent

    def main(self):
        headers = {
            'Host': 'www.kuaidi100.com',
            'Referer': 'https: // www.kuaidi100.com /?from=openv',
            'X-Requested-With': 'XMLHttpRequest',
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'
            # 'User-Agent': self.user_agent
        }
        try:
            # proxies = {'http': self.ip, 'https': self.ip}
            response = requests.post(url=self.url, headers=headers).json()
            return response
            if response.get("auto") == []:
                print("请使用正确的快递号！")
                return None
            else:
                if response.get("message") == '非法IP':
                    return ''
                else:
                    return response.get("auto")[0].get("comCode")
        except:
            return ''


if __name__ == '__main__':
    with open('kuaidi_data.txt','r') as f:
        datas = f.readlines()
    xx = open('result.txt','a+')
    for data in datas:
        deliver_num = data
        result = kuaidi_auto(deliver_num).main()
        print(result)
        xx.write(str(result)+'\n')
        # time.sleep(0.5)
    xx.close()